<!DOCTYPE html>
<?php
require_once("DB_Config.php");
$db_connect = DB_Connection::getInstance();
?>

<html>
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=8" />
<meta http-equiv="X-UA-Compatible" content="IE=9" />
<link href="css/style.css" rel='stylesheet' type='text/css' />
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/jquery-ui.min.js"></script>
<script type="text/javascript" src="js/jquery.package-slider.js"></script>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script>
<script>
  function setDate(){ 
    var dt = new Date();
    var dtstring = dt.getFullYear() + '-' + (dt.getMonth()+1) + '-' + (dt.getDate())+ ' ' + (dt.getHours())+ ':' + (dt.getMinutes())+ ':' + (dt.getSeconds());
    var elem = document.getElementById("localDate");
    elem.value = dtstring;
    }


    window.onload =  function MoveScrollBarDown() {
          var chatbox = document.getElementById('txt_Chat');
          chatbox.scrollTop = chatbox.scrollHeight;
      }

       

</script>

<title>A&A ONLINE UNIVERSITY</title>
</head>

<body>
  <!-- START HEADER WRAPER-->
  <div id="header-color">
    <div id="header-wrapper">
      <?php require_once("header.php"); 
      if(isset($_SESSION['myusername'])==FALSE)  { header("location:index.php");} 
      else {?> <b class="hellouser"> Hello <?php echo $_SESSION['myusername']; ?></b> <?php } ?>
    </div>
  </div>

 <!-- Insert sent msg into db -->
  <?php if( (isset($_POST['send_msg'])) && ( $_POST['send_msg_content'] != "" ) ){ 
    $content = htmlentities($_POST['send_msg_content']);
    $localDate = htmlentities($_POST['localDate']);
    $sql = "INSERT INTO messages(msg_from, msg_to, msg_time, msg_content) VALUES (?,?,?,?)";
    $stmt = $db_connect->db_conn->prepare($sql);
    $param=array($_SESSION['myusername'], $_POST['msg_with'], $localDate,$content);
    $stmt->execute($param);
  }?>



  <!-- START CONTENT WRAPER-->
  <div id="page-wrapper">

  <div id="div_for_map" style="width: 900;  background-color: rgba(120, 158, 19, 0.72)">

  <table dir="ltr" width="750" align="center" border="0" cellpadding="0" cellspacing="0" bgcolor="#F1F1F1">
    <tr class="message_td">
      <td width="550" height="560">
        <p id="txt_Chat"; style="width:639px; height:543px; word-wrap:normal; overflow:auto; background-color: rgb(184, 236, 199);">
          <?php
          $sql="SELECT * FROM messages WHERE msg_from=? OR msg_to=? ORDER BY msg_time";
          $stmt = $db_connect->db_conn->prepare($sql);
          $param=array($_SESSION['myusername'],$_SESSION['myusername']);
          if ($stmt->execute($param)){
            $res = $stmt->fetchAll(PDO::FETCH_ASSOC);
            $i = 0; // while index
            $user_talk = array();
            $flag = 0; // duplication indicator
            $arrlength = 0; // number of conversations
            $user_talk[] = "$arrlength";// $arrlegnth = index


            // Creates Array (user_talk[]) For Conversation List
            if ( $_SESSION['myusername'] != $res[0]['msg_from'] ){$user_talk["$arrlength"] = $res[0]['msg_from'];}
            else { $user_talk["$arrlength"] = $res[0]['msg_to']; }
            while ($i < count($res)) {

              for( $x=0; $x<$arrlength+1 ;$x++ ){
                if ( ($user_talk["$x"] == $res[$i]['msg_from']) || ($_SESSION['myusername'] == $res[$i]['msg_from']) ){ 
                  $flag=1; 
                }
              }
              if ( $flag == 0 ){ $arrlength++; $user_talk[] = "$arrlength";  $user_talk["$arrlength"] = $res[$i]['msg_from'];   }
              $flag = 0;

              for( $x=0; $x<$arrlength+1 ;$x++ ){
                if ( ($user_talk["$x"] == $res[$i]['msg_to']) || ($_SESSION['myusername'] == $res[$i]['msg_to']) ){ 
                  $flag=1; 
                }
              }
              if ( $flag == 0 ){ $arrlength++; $user_talk[] = "$arrlength";  $user_talk["$arrlength"] = $res[$i]['msg_to'];   }
              $flag = 0;
              $i++;
            }          
          }


          // Shows Massages With Choosen User - msg_with (User Button Clicked)
          if( isset($_POST['msg_with']) ){
            $sql="SELECT * FROM messages WHERE msg_from=? OR msg_to=? ORDER BY msg_time";
            $stmt = $db_connect->db_conn->prepare($sql);
            $param=array($_SESSION['myusername'],$_SESSION['myusername']);
            if ($stmt->execute($param)){
              $res = $stmt->fetchAll(PDO::FETCH_ASSOC);
              $i = 0; 
              while ($i < count($res)) {
                if ( ($res[$i]['msg_from'] == $_SESSION['myusername']) && (($res[$i]['msg_to'] == $_POST['msg_with'])) ){ 
                  ?> <font size="3" face="verdana" color="blue">&nbsp <?php echo $_SESSION['myusername']; ?>: </font><br> <?php
                  ?> <font size="3" face="verdana" color="black">&nbsp  <?php echo $res[$i]['msg_content']; ?> </font><br><br> <?php
                }elseif ( ($res[$i]['msg_to'] == $_SESSION['myusername']) && (($res[$i]['msg_from'] == $_POST['msg_with'])) ){
                  ?> <font size="3" face="verdana" color="red">&nbsp <?php echo $res[$i]['msg_from']; ?>: </font><br> <?php
                  ?> <font size="3" face="verdana" color="black">&nbsp <?php echo $res[$i]['msg_content']; ?> </font><br><br> <?php
                }
                $i++;
              }
            }
          } ?>
        </p>
      </td>

      <!-- Shows Conversation Users (Right Corner) -->
      <td width="200"  rowspan="2" style="background-color: gray;">
        <div style="width: 111px; height: auto; background-color: rgb(133, 133, 133); margin: 0px 0px 53px 0px;" font-size="12" color="rgb(0, 133, 133);">
          <form name="show_msg" method="post" action="messages.php">
            <?php for( $x=0; $x<$arrlength+1 ;$x++ ){ ?>
                    <input name="msg_with" type="submit" value="<?php echo $user_talk["$x"]; ?>" class="button" id="search_but" />
            <?php } ?>

<?php// for( $x=0; $x<20 ;$x++ ){ ?>
<!-- <input type="submit" value="Send" name="send_msg" class="button" id="search_but" onClick="setDate()"> -->
<?php //} ?>


          </form>
        </div>
      </td>
    </tr>
    <tr>
      <td>

        <!-- Text Area -->
        <form name="sending_msg" method="post" action="messages.php">     
          <TEXTAREA name="send_msg_content" style="width: 633px; height: 90px;"></TEXTAREA>
          <?php if( isset($_POST['msg_with']) ){ ?>
                
                <input type="submit" value="Send" name="send_msg" class="button" id="search_but" onClick="setDate()">
                <input type="hidden" value="<?php echo $_POST['msg_with']; ?>" name="msg_with">
                <input type="hidden" id="localDate" name="localDate" value="">
          <?php } ?> 
        </form>
      </td>
    </tr>
  </table>
</div>

</div>
<!-- END page-wrapper-->
<!-- START FOOTER WRAPER-->
<p><?php require("footer.php"); ?></p>
</body>
</html>

